package com.zq.springadmin.controller;


import com.github.pagehelper.PageInfo;
import com.zq.springadmin.dao.dto.LoginDto;
import com.zq.springadmin.dao.dto.SysUserDto;
import com.zq.springadmin.dao.entity.User;
import com.zq.springadmin.dao.vo.SysUserInfoVo;
import com.zq.springadmin.logs.sysLogs;
import com.zq.springadmin.service.IUserService;
import com.zq.springadmin.util.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张乔
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/sys/user")
@Slf4j
public class UserController {

    @GetMapping("/test")
    public String testhello(){
        System.out.println("sys系统中的hello");
        return "testSuccess";
    }


    @Resource
    private IUserService userService;

    //登录
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDto loginDto){
        String token = userService.login(loginDto);
        return Result.successData(token);
    }

    //获取用户信息
@GetMapping("/getUserInfo")
public Result<SysUserInfoVo> getSysUSerInfo(@RequestHeader("token") String token){
//    System.out.println("token=================>"+token);
    SysUserInfoVo sysUser=userService.getSysUSerInfo(token);
    return Result.successData(sysUser);
}





//修改用户头像
@PostMapping("/updateAvatar")
public Result<String> updateAvatar(MultipartFile file, @RequestHeader("token") String token){
    String url=    userService.updateAvatar(file,token);
//        String url = minioService.uploadImage(minioProperties.getBucketName(),file);
    return Result.successData(url);
}
//退出登录
@GetMapping("/logout")
public Result<String> logout(@RequestHeader("token")String token){
    userService.logout(token);
    return Result.success();
}

//修改用户的信息
    @PostMapping("/updateUserInfo")
    public Result<String> updateUserInfo(@RequestBody User user, @RequestHeader("token") String token){
        userService.updateUserInfo(user,token);
        return Result.success();
    }

//    新增用户
    @PostMapping("/addUser")
    @sysLogs(title = "用户管理：新增用户",businessType = 1)
    public Result<String> addUser(@RequestBody User user){
        System.out.println("user=================>"+user);
        userService.addUser(user);
        return Result.success();
    }

//获取全部用户
    @GetMapping("/getAllUser")
    public Result<List<User>> getAllUser(){
        List<User> userList = userService.getAllUser();
        return Result.successData(userList);
    }

//分页条件查询
    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<Object> findByPage( SysUserDto sysUserDto ,
                                     @PathVariable(value = "pageNum") Integer pageNum ,
                                     @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<User> pageInfo =userService.findByPage(sysUserDto,pageNum,pageSize);
return     Result.successData(pageInfo);

    }

//    修改指定用户信息
    @PutMapping(value = "/updateSysUser")
    @sysLogs(title = "用户管理：修改用户",businessType = 2)
    public Result<String> UpdateSysUser(@RequestBody User user){
        userService.updateSysUser(user);
        return Result.success();
    }
//    根据id删除用户
    @DeleteMapping(value = "/deletedById/{id}")
    @sysLogs(title = "用户管理：删除用户",businessType = 1)
    public Result<String> deletedById(@PathVariable("id") Integer id){
        userService.deletedById(id);
        return Result.success();
    }









}





